package com.hh.legalFactory.service.impl;/*
 * @Auther:ZhengXueCheng
 * @Date:2023/7/27
 * @VERSON:1.8
 */

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hh.commen.PageVO;
import com.hh.legalFactory.bean.Factory;
import com.hh.legalFactory.bean.QueryPlantByLegalVo;
import com.hh.legalFactory.mapper.FactoryMapper;
import com.hh.legalFactory.mapper.LegalPersonMapper;
import com.hh.legalFactory.service.LegalFactoryService;
import com.hh.legalFactory.vo.AddLegalPlantVo;
import com.hh.legalFactory.vo.FormInlineVo;
import com.hh.legalFactory.vo.ModifyLegalPlantVo;
import com.hh.legalFactory.vo.UpdateStatusVo;
import com.hh.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class LegalFactoryServiceImpl implements LegalFactoryService {
    @Autowired
    private FactoryMapper factoryMapper;
    @Autowired
    private LegalPersonMapper legalPersonMapper;

    @Override
    public PageVO findAllByPage(FormInlineVo formInlineVo) {
        //设置pagehelper的开始页和每页的大小
        PageHelper.startPage((formInlineVo.getPageIndex()), formInlineVo.getPageSize());
        //获得将要分页的数据集合列表
        List<Factory> factoryList = factoryMapper.selectAllByPage(formInlineVo);
        //初始化pagehelper的数据信息
        PageInfo<Factory> pageInfo = new PageInfo<>(factoryList);
        PageVO pageVO = new PageVO();
        pageVO.setData(factoryList);
        //通过pageInfo对象获取到总条数
        pageVO.setTotalNumber(pageInfo.getTotal());
        //通过pageInfo对象获取到总页数
        pageVO.setTotalPage(pageInfo.getPageNum());
        return pageVO;
    }

    /**
     * 更新工厂的状态信息
     *
     * @param updateStatusVo
     * @return
     */
    @Override
    public ResultVo updateStatus(UpdateStatusVo updateStatusVo) {
        if (factoryMapper.judgeLegalPersonStatus(updateStatusVo.getLegalPersonCode()) == 0) {
            return ResultVo.error("此工厂法人已经禁用，工厂无法启用");
        }
        if (factoryMapper.updateStatusById(updateStatusVo) == 0) {
            return ResultVo.error("无法更新状态");
        }
        return ResultVo.success();
    }

    /**
     * 获取法人信息列表
     *
     * @return
     */
    @Override
    public ResultVo findAllLegalPersonName() {
        return ResultVo.success(legalPersonMapper.selectLegalNameList("1"));
    }

    /**
     * 添加工厂信息
     *
     * @param addLegalPlantVo
     * @return
     */
    @Override
    public ResultVo addFactory(AddLegalPlantVo addLegalPlantVo) {
        if (factoryMapper.judgeFactoryCode(addLegalPlantVo.getLegalPlantCode()) > 0) {
            return ResultVo.error("工厂编码重复");
        }
        Factory factory = new Factory();
        factory.setFactoryCode(addLegalPlantVo.getLegalPlantCode());
        factory.setRemark(addLegalPlantVo.getLegalPlantDec());
        //前端传来的enable属性时Boolean类型，将其转化为Integer类型
        if (addLegalPlantVo.getEnabled()) {
            factory.setStatus(1);
        } else {
            factory.setStatus(0);
        }
        factory.setLegalPersonCode(addLegalPlantVo.getLegalPersonCode());
        //获取当前时间，设置创建时间
        Date date = new Date();
        factory.setCreateTime(date);
        factory.setUpdateTime(date);
        factoryMapper.insert(factory);
        return ResultVo.success();
    }

    @Override
    public ResultVo updateFactory(ModifyLegalPlantVo modifyLegalPlantVo) {
        if (factoryMapper.judgeLegalPersonStatus(modifyLegalPlantVo.getLegalPersonCode()) == 0) {
            return ResultVo.error("此工厂法人已经禁用，工厂无法启用");
        }
        if (factoryMapper.judgeUpdateFactoryName(modifyLegalPlantVo.getId(), modifyLegalPlantVo.getLegalPlantCode()) > 0) {
            return ResultVo.error("工厂编码不能重复");
        }
        Factory factory = new Factory();
        factory.setId(modifyLegalPlantVo.getId());
        factory.setStatus(modifyLegalPlantVo.getEnabled());
        factory.setFactoryCode(modifyLegalPlantVo.getLegalPlantCode());
        factory.setRemark(modifyLegalPlantVo.getLegalPlantDec());
        factory.setLegalPersonCode(modifyLegalPlantVo.getLegalPersonCode());
        factory.setLegalPersonName(modifyLegalPlantVo.getLegalPersonName());
        Date date = new Date();
        factory.setUpdateTime(date);
        factory.setVersion(modifyLegalPlantVo.getVersion());
        factoryMapper.updateFactoryByPrimaryKey(factory);
        return ResultVo.success();
    }

    @Override
    public ResultVo getPlanByLegalCode(String legalPersonCode) {
        List<QueryPlantByLegalVo> plants =factoryMapper.getPlanByLegalCode(legalPersonCode);
        return ResultVo.success(plants);
    }


}
